package cn.my.framework.config;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by zhangl_lc on 2017/7/5.
 */
@Aspect
@Component
public class MyWebLogAspect {

    Logger logger = LoggerFactory.getLogger(MyWebLogAspect.class);

    @Pointcut(value = "@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    //@Pointcut(value = "execution(public * cn.my.framework..*.*(..))")
    public void webControllerLog(){}

    //s@Before("webControllerLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if(attributes != null){
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            /*logger.info("URL : " + request.getRequestURL().toString());
            logger.info("HTTP_METHOD : " + request.getMethod());
            logger.info("IP : " + request.getRemoteAddr());
            logger.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
            logger.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));*/
        }
    }

    //@Around("webControllerLog()")
    public Object controllerAroundLog(ProceedingJoinPoint jp) throws Throwable {
        //do something
        //logger.info("...... before .. signature:{}",jp.getSignature());
        Object result = jp.proceed();
        //logger.info("...... after .. result:{}",result);
        //do something
        return result;
    }
}
